package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IAccountDao;
import com.example.demo.dao.IEmailDao;
import com.example.demo.dao.IVpsDao;
import com.example.demo.jooq.tables.daos.*;
import com.example.demo.jooq.tables.pojos.*;
import com.example.demo.model.po.AccountPage;
import com.example.demo.model.po.VpsPage;
import com.example.demo.model.request.AccountPageRequest;
import com.example.demo.model.request.AccountRequest;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.model.response.AccountPageResponse;
import com.example.demo.model.response.AccountResponse;
import com.example.demo.model.response.VpsResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IAccountService;
import com.example.demo.service.order.IVpsService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IAccountServiceImpl extends BaseBusinessServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao iAccountDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private EmailDao emailDao;

    @Autowired
    private VpsDao vpsDao;

    @Autowired
    private AddressDao addressDao;
    @Autowired
    private VisaCardDao visaCardDao;
    @Autowired
    private IEmailDao iEmailDao;
    @Autowired
    private VirtualDao virtualDao;
    @Override
    public ResultObject<BasePageResponse<AccountPageResponse>> queryPageAccountList(AccountPageRequest accountPageRequest) {
        ResultObject<BasePageResponse<AccountPageResponse>> responseResultObject = new ResultObject<>();
        List<AccountPage> vpsPages =  iAccountDao.queryPageAccountList(accountPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<AccountPageResponse> vpsResponses = (List<AccountPageResponse>)this.converterList(vpsPages,new TypeToken<List<AccountPageResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getAccountStatus())){
                    Arrays.stream(TerminologyConfigEnum.ACCOUNT_STATUS.values()).filter(value->value.name().equals(visaResponse.getAccountStatus())).forEach(value->visaResponse.setAccountStatus(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getAccountUseType())){
                    Arrays.stream(TerminologyConfigEnum.ACCOUNT_USE_TYPE.values()).filter(value->value.name().equals(visaResponse.getAccountUseType())).forEach(value->visaResponse.setAccountUseType(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getSite())){
                    Arrays.stream(TerminologyConfigEnum.SITE.values()).filter(value->value.name().equals(visaResponse.getSite())).forEach(value->visaResponse.setSite(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getStar())){
                    Arrays.stream(TerminologyConfigEnum.STAR.values()).filter(value->value.name().equals(visaResponse.getStar())).forEach(value->visaResponse.setStar(value.desc()));
                }
            });
            BasePageResponse<AccountPageResponse> responseBasePageResponse =BasePageResponse.getData(accountPageRequest.getCurrentPage(),accountPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }

    @Override
    public ResultObject saveAccount(AccountRequest accountRequest) {
        this.getLogger().info("保存账户,请求数据为:{}", JSON.toJSONString(accountRequest));
        ResultObject resultObject = new ResultObject();
        //参数校验
        vaildAccountParameter(accountRequest);
        Account account =null;
        if(AssertUtils.isNotNull(accountRequest.getAccountId())){
            account=accountDao.fetchOneByAccountId(accountRequest.getAccountId());
            AssertUtils.isNotNull(this.getLogger(),account, BaseErrorConfigEnum.ACCOUNT_NOT_NULL);
        }else {
            account=new Account();
        }
        ClazzUtils.copyPropertiesIgnoreNull(accountRequest,account);
        //持久化
        if(AssertUtils.isNotEmpty(accountRequest.getAccountId())){
            account.setUpdatedDate(System.currentTimeMillis());
            accountDao.update(account);
        }else {
            account.setAccountStatus(TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            account.setAccountId(DateUtils.getJobNumberByTime2("ACCOUNT",null,DateUtils.FORMATE53,false));
            account.setCreatedDate(System.currentTimeMillis());
            accountDao.insert(account);
        }
        //关联email
        if(AssertUtils.isNotNull(account.getEmail())){
            Email email = iEmailDao.queryOneEmail(account.getEmail());
            AssertUtils.isNotNull(this.getLogger(),email,BaseErrorConfigEnum.EMAIL_NOT_NULL);
            if(TerminologyConfigEnum.ACCOUNT_STATUS.DISABLE.name().equals(account.getAccountStatus())){
                email.setEmailStatus(TerminologyConfigEnum.EMAIL_STATUS.DISABLE.name());
            }else {
                email.setEmailStatus(TerminologyConfigEnum.EMAIL_STATUS.USEING.name());
            }
            emailDao.update(email);
        }
        //关联vps
        if(AssertUtils.isNotNull(account.getVpsId())){
            Vps vps = vpsDao.fetchOneByVpsId(account.getVpsId());
            AssertUtils.isNotNull(this.getLogger(),vps,BaseErrorConfigEnum.VPS_NOT_NULL);
            if(TerminologyConfigEnum.ACCOUNT_STATUS.DISABLE.name().equals(account.getAccountStatus())){
                vps.setVpsStatus(TerminologyConfigEnum.EMAIL_STATUS.DISABLE.name());
            }else {
                vps.setVpsStatus(TerminologyConfigEnum.EMAIL_STATUS.USEING.name());
            }
            vpsDao.update(vps);
        }
        //关联地址
        if(AssertUtils.isNotNull(account.getAddressId())){
            Address address = addressDao.fetchOneByAddressId(account.getAddressId());
            AssertUtils.isNotNull(this.getLogger(),address,BaseErrorConfigEnum.ADDRESS_NOT_NULL);
            if(TerminologyConfigEnum.ACCOUNT_STATUS.DISABLE.name().equals(account.getAccountStatus())){
                address.setAddressStatus(TerminologyConfigEnum.ADDRESS_STATUS.DISABLE.name());
            }else {
                address.setAddressStatus(TerminologyConfigEnum.ADDRESS_STATUS.USEING.name());
            }

            addressDao.update(address);
        }
        //虚拟机
        if(AssertUtils.isNotNull(account.getVirtualId())){
            Virtual virtual = virtualDao.fetchOneByVirtualId(account.getVirtualId());
            AssertUtils.isNotNull(this.getLogger(),virtual,BaseErrorConfigEnum.VIRTUAL_NOT_NULL);
            if(TerminologyConfigEnum.ACCOUNT_STATUS.DISABLE.name().equals(account.getAccountStatus())){
                virtual.setVirtualStatus(TerminologyConfigEnum.VIRTUAL_STATUS.DISABLE.name());
            }else {
                virtual.setVirtualStatus(TerminologyConfigEnum.VIRTUAL_STATUS.USEING.name());
            }
            virtualDao.update(virtual);
        }
        //关联卡
        if(AssertUtils.isNotNull(account.getVisaCardId())){
            VisaCard visaCard = visaCardDao.fetchOneByVisaCardId(account.getVisaCardId());
            AssertUtils.isNotNull(this.getLogger(),visaCard,BaseErrorConfigEnum.CARD_NOT_NULL);
            if(TerminologyConfigEnum.ACCOUNT_STATUS.DISABLE.name().equals(account.getAccountStatus())){
                visaCard.setCardStatus(TerminologyConfigEnum.CARD_STATUS.DISABLE.name());
            }else {
                visaCard.setCardStatus(TerminologyConfigEnum.CARD_STATUS.USEING.name());
            }
            visaCardDao.update(visaCard);
        }

        return resultObject;
    }

    private void vaildAccountParameter(AccountRequest accountRequest) {
        AssertUtils.isNotNull(this.getLogger(),accountRequest.getAccountUseType(), BaseErrorConfigEnum.ACCOUNT_USE_TYPE_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),accountRequest.getChargeUserName(), BaseErrorConfigEnum.ACCOUNT_CHANGE_USER_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),accountRequest.getEmail(), BaseErrorConfigEnum.EMAIL_DETAIL_IS_NOT_NULL);
        //AssertUtils.isNotNull(this.getLogger(),accountRequest.getVpsId(), BaseErrorConfigEnum.ACCOUNT_VPS_IS_NOT_NULL);
        //AssertUtils.isNotNull(this.getLogger(),accountRequest.getVirtualId(), BaseErrorConfigEnum.ACCOUNT_VIRTUAL_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),accountRequest.getStar(), BaseErrorConfigEnum.ACCOUNT_STAR_IS_NOT_NULL);
    }

    @Override
    public ResultObject<AccountPageResponse> queryAccountDetail(String accountId) {
        ResultObject<AccountPageResponse> resultObject=new ResultObject<>();
        AccountPage accountPage = iAccountDao.queryAccountDetail(accountId);
        if(AssertUtils.isNotNull(accountPage)){
            AccountPageResponse accountPageResponse=(AccountPageResponse)this.converterObject(accountPage,AccountPageResponse.class);
            resultObject.setData(accountPageResponse);
        }
        return resultObject;
    }

}
